package com.ml.carManager.ui.dialog;

import android.animation.Animator;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


import com.ml.carManager.R;
import com.ml.carManager.adapter.AnimatorFinshAdapter;
import com.ml.carManager.ui.dialog.widgets.CircularProgress;

import java.util.ArrayList;
import java.util.List;

import custom.base.ui.decoration.DecorationLine;
import custom.base.utils.DensityUtil;
import custom.base.utils.ViewPrepared;

/**
 * Created by dundun on 2016/11/22.
 */

public class ChoiceListDialog extends Dialog implements View.OnClickListener, View.OnTouchListener {
    private RelativeLayout rlBgView;
    private LinearLayout llDownLayout;
    private RecyclerView recyclerView;
    private TextView tvCancel;
    private CircularProgress circularProgress;

    protected List<String> list = new ArrayList<>();
    private ChoiceListAdapter choiceListAdapter;

    private boolean ISANIMING = false;
    private static int DURATION_TIME = 300;

    public ChoiceListDialog(Context context) {
        super(context, R.style.trans_dialog);
    }

    public ChoiceListDialog(Context context, List<String> list) {
        super(context, R.style.trans_dialog);
        this.list = list;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);//状态栏透明
//        StatusBarUtil.setTranslucent(getOwnerActivity());
        setContentView(R.layout.dialog_choice_list);
        initView();
        initData();
        initListener();
    }

    private void initView() {

        rlBgView = findViewById(R.id.dialog_choice_list_bg);
        llDownLayout = findViewById(R.id.dialog_choice_list_down_layout);
        recyclerView = findViewById(R.id.dialog_choice_list_recyclerview);
        tvCancel = findViewById(R.id.dialog_choice_list_cancel);
        circularProgress = findViewById(R.id.dialog_choice_list_progress);

        tvCancel.setOnClickListener(this);
    }

    private void initData() {
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.addItemDecoration(new DecorationLine(DecorationLine.VERTICAL_LIST, DensityUtil.dip2px(getContext(), 20),
                DensityUtil.dip2px(getContext(), 20)));

        choiceListAdapter = new ChoiceListAdapter(list);
        recyclerView.setAdapter(choiceListAdapter);

//        animIn();
    }

    /**
     * 为空的时展示等待的界面，不为空时展示列表,本弹窗可以异步展示列表
     */
    public void show(List<String> list) {
        if (list == null)
            return;
        this.list.clear();
        this.list.addAll(list);
        show();
    }

    @Override
    public void show() {
        super.show();
        if (list == null || list.size() == 0) {
            circularProgress.setVisibility(View.VISIBLE);
            recyclerView.setVisibility(View.GONE);
            return;
        }
        circularProgress.setVisibility(View.GONE);
        recyclerView.setVisibility(View.VISIBLE);

        if (list.size() >= 7) {
            ViewGroup.LayoutParams layoutParams = recyclerView.getLayoutParams();
//            layoutParams.height = DensityUtil.dip2px(getContext(), 44) * 7;
            layoutParams.height = DensityUtil.dip2px(getContext(), 300);
            recyclerView.setLayoutParams(layoutParams);
        } else {
            ViewGroup.LayoutParams layoutParams = recyclerView.getLayoutParams();
            layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
            recyclerView.setLayoutParams(layoutParams);
        }

        choiceListAdapter.notifyDataSetChanged();

        new ViewPrepared().asyncPrepare(llDownLayout, new ViewPrepared.OnPreDrawFinishListener() {
            @Override
            public void onPreDrawFinish(int w, int h) {
                llDownLayout.setTranslationY(h);
                animIn();
            }
        });

    }

    private void initListener() {
        rlBgView.setOnTouchListener(this);
    }


    @Override
    public void onClick(View v) {
        if (v.getId() == tvCancel.getId()) {
            animOut();
        }
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_UP)
            animOut();
        return true;
    }

    /**
     * anim in dialog
     */
    private void animIn() {
        llDownLayout.animate().translationY(0).setDuration(DURATION_TIME).setListener(new AnimatorFinshAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                ISANIMING = false;
            }
        }).start();

        rlBgView.setAlpha(1f);
        Animation alpha = AnimationUtils.loadAnimation(getContext(), R.anim.anim_fade_in);
        alpha.setDuration(DURATION_TIME);
        rlBgView.startAnimation(alpha);
    }


    /**
     * anim out finish
     */
    public void animOut() {
        if (ISANIMING) return;
        ISANIMING = true;
        rlBgView.animate().alpha(0).setDuration(DURATION_TIME).start();

        llDownLayout.animate().translationY(llDownLayout.getHeight()).setDuration(DURATION_TIME).setListener(new AnimatorFinshAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                ISANIMING = false;
                dismiss();
            }
        }).start();
    }

    @Override
    public void onBackPressed() {
        animOut();
    }

    private OnChoiceListItemClickListener onChoiceListItemClickListener = null;

    public void setOnChoiceListItemClickListener(OnChoiceListItemClickListener onChoiceListItemClickListener) {
        this.onChoiceListItemClickListener = onChoiceListItemClickListener;
    }

    public interface OnChoiceListItemClickListener {
        void onListItemClick(int index);
    }


    public class ChoiceListAdapter extends RecyclerView.Adapter<ChoiceListAdapter.MyHolder> {
        public class MyHolder extends RecyclerView.ViewHolder {
            public final TextView title;

            public MyHolder(View v) {
                super(v);
                title = (TextView) v.findViewById(R.id.view_choice_item_name);
            }
        }

        private List<String> mDatas;

        public ChoiceListAdapter(List<String> data) {
            this.mDatas = data;
        }

        @Override
        public void onBindViewHolder(MyHolder holder, final int position) {
            holder.title.setText(mDatas.get(position));
            holder.title.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    animOut();
                    if (onChoiceListItemClickListener != null) {
                        onChoiceListItemClickListener.onListItemClick(position);
                    }
                }
            });
        }

        @Override
        public int getItemCount() {
            return mDatas.size();
        }

        @Override
        public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            //LayoutInflater.from指定写法
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.view_choice_item, parent, false);
            return new MyHolder(v);
        }
    }


}
